/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function removeFromArray(myArray, Value) {


    var returnData = "";

    for (i = 0; i < myArray.length; i++) {


        if (myArray[i] != Value) {

            returnData += "__" + myArray[i];

        }

        return returnData.split("__");

    }





}

function getoptions(seleected, arrOptionsvalue, arrOptionstext, removeValue) {

    var returnData = "";

    for (r = 0; r < removeValue.length; r++) {
        if (removeValue[r] != "") {
            var idx = arrOptionsvalue.indexOf(removeValue[r]);

            if (idx != -1) {
                arrOptionsvalue.splice(idx, 1);
            }

            var idxt = arrOptionstext.indexOf(removeValue[r]);
            if (idxt != -1) {
                arrOptionstext.splice(idxt, 1);
            }
        }
    }



    for (i = 0; i < arrOptionsvalue.length; i++) {

        if (arrOptionstext[i] != "") {

            var sel = "";

            if (seleected == arrOptionsvalue[i]) {
                sel = "selected='selected'";

            } else {

                sel = "";
            }

            returnData += "<option " + sel + " value='" + arrOptionsvalue[i] + "'>" + arrOptionstext[i] + "</option>";

        }

    }

    return  returnData;
}

function dooptions(arrOptions) {
    // tds=createSelect($(".modleName").val(),"Name_Sql_Name", 'width:100px;',dooptions());

    var myselect = document.createElement('select');

    for (i = 0; i < arrOptions.length; i++) {


        var paperSizeA4Option = document.createElement('option');
        paperSizeA4Option.setAttribute('value', arrOptions[i]);
        mxUtils.write(paperSizeA4Option, arrOptions[i]);
        myselect.appendChild(paperSizeA4Option);


    }








    return myselect;


}

function  getAllNodeText(graph) {

    var returdata = "";
    var cells = graph.getChildCells(graph.getDefaultParent(), true, true);
    for (var i = 0; i < cells.length; i++) {
        if (!cells[i].isEdge()) {


            returdata += cells[i].getValue() + "__";


        }
    }


    return  returdata.split("__");



}

function getManagedItemsXML() {


    var returnData = "";


    $(".top_Managed_Item_more").each(function() {

        var mvalue = getValueDataByValue($(this).val(), 1)[0];

        if (mvalue != null && mvalue != undefined && mvalue != "undefined") {
            returnData += '<migration-item>\n\
<item-type name="' + getValueDataByValue($(this).val(), 1)[0] + '" sql-name="' + getValueDataByValue($(this).val(), 1)[1] + '" collision-window="' + getValueDataByValue($(this).val(), 1)[2] + '">\n\
<system-entity system="' + getValueDataByValue($(this).val(), 1)[3] + '">' + getValueDataByValue($(this).val(), 1)[4] + '</system-entity>\n\
</item-type></migration-item>';

        }

    });




    return returnData;



}

function getmDirectionXml() {

    var value = getManagedItemsXML();



    $.post("ajax/createSession.jsp", {
        data: value,
        type: "ManagedItems"
    },
            function(data) {

                /*  $.get("ajax/creatXmlfile.jsp", function() {
                 
                 window.open(
                 'data.xml',
                 '_blank' // <- This is what makes it open in a new window.
                 );
                 
                 
                 });*/
            });


    return value;

}

function removeRow(th) {
    //System

    var myvalue = $(th).parent().parent(".morehere").children("td").children(".Entity").val();
    $('.Entity option[value="' + myvalue + '"]').remove();

    if (myvalue != null && myvalue != "" && myvalue != "null") {
        $(".Entity").append("<option value='" + myvalue + "'>" + myvalue + "</option>");
    }
    $(th).parent().parent(".morehere").remove();
    updateEntity();

}

function updateEntity(mvalue, mvatext) {



    var data = [];
    var i = 0;

    $(".Entity").each(function() {
        if (mvalue.in)
            $(this).append(getoptions("", mvalue, mvalue, ""));
    });


    $(".Entity").each(function() {
        var usedNames = {};
        $(this).children("option").each(function() {
            if (usedNames[this.text]) {
                $(this).remove();
            } else {
                usedNames[this.text] = this.value;
            }
        });
    })


    $(".Entity").each(function() {

        var myval = $(this).val();
        if (myval != null && myval != "" && myval != "null") {

            if (data.indexOf(myval) == -1) {

                $(".Entity").not(this).children('option[value="' + myval + '"]').remove();
                $(".Entity").not(this).children('option[value="' + myval + '"]').remove();
                data.push(myval);

            } else {

                $(this).val("");
                $(this).children('option[value="' + myval + '"]').remove();
            }

        }


    });







}

function dataForRow(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue, th, systems) {


    var sqlNameTests = sqlNameTest.split("__");
    var sqlNameValues = sqlNameValue.split("__");
    var systemEntityTexts = systemEntityText.split("__");
    var systemEntityValues = systemEntityValue.split("__");
    var graphtextes = systems;

    var systemEntityTexts_data = ""
    for (var r = 0; r < systemEntityTexts.length; r++) {
        systemEntityTexts_data += systemEntityTexts[r] + "__";
    }
    var systemEntityTextsv = systemEntityTexts_data.split("__");



    var systemEntityValues_data = ""
    for (var e = 0; e < systemEntityValues.length; e++) {
        systemEntityValues_data += systemEntityValues[e] + "__";
    }


    var systemEntityValuesv = systemEntityValues_data.split("__");


    var eData = ""
    $(".Entity").each(function() {
        eData += $(this).val() + "__";
    });
    var eDataArray = eData.split("__");


    var beforchang = "";
    $(".Entity").click(function() {
        beforchang = $(this).val();
    });




    $(".Entity").change(function() {

        updateEntity(systemEntityTextsv, systemEntityValuesv);



    });

    var retunData = "";



    if (th != "xx") {

        var myvals = getValueDataByValue(th.val(), 1);

        if (myvals[0] != null && myvals[0] != undefined && myvals[0] != "undefined") {
            retunData += "<tr class='morehere'>\n\
<td><input class='Managed_Item_Name' value='" + myvals[0] + "' style='width:100px'/></td>\n\
<td><select class='Name_Sql_Name'   style='width:180px'><option></option>" + getoptions(myvals[1], sqlNameValues, sqlNameTests, "") + "</select></td>\n\
<td><input class='Collsion' value='" + myvals[2] + "' style='width:100px'/></td>\n\
<td><select  class='System'  style='width:150px' ><option></option>" + getoptions(myvals[3], graphtextes, graphtextes, "") + "</select></td>\n\
<td><select class='Entity'  style='width:150px'><option></option>" + getoptions(myvals[4], systemEntityTextsv, systemEntityValuesv, eDataArray) + "</select></td><td><input type='button' onclick='javascript:removeRow(this)' class='rowRemove'value='X' /></td></tr>";
        }


    } else {

        retunData += "<tr class='morehere'>\n\
<td><input class='Managed_Item_Name' style='width:100px'/></td>\n\
<td><select class='Name_Sql_Name'  style='width:180px'><option></option>" + getoptions("", sqlNameValues, sqlNameTests, "") + "</select></td>\n\
<td><input class='Collsion'  value='120' style='width:100px'/></td>\n\
<td><select  class='System'  style='width:150px' ><option></option>" + getoptions("", graphtextes, graphtextes, "") + "</select></td>\n\
<td><select class='Entity'  style='width:150px'><option></option>" + getoptions("", systemEntityTextsv, systemEntityValuesv, eDataArray) + "</select></td><td><input type='button' onclick='javascript:removeRow(this)' class='rowRemove'value='X' /></td></tr>";
    }


    return retunData;


}

function me_doOnLoad(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue) {






    var name = "topolgy_systems";

    $(".top_Managed_Item_more").each(function() {


        var myob = $(this);
        getSesstion(name, function(mvalue) {


            mvalue = mvalue.split("__");

            $(".morehere:last").after(dataForRow(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue, myob, mvalue));


        })



    });

    if ($(".top_Managed_Item_more").size() == 0) {

        getSesstion(name, function(mvalue) {
            mvalue = mvalue.split("__");

            $(".morehere:last").after(dataForRow(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue, "xx", mvalue));

        })

    }







}

function UpdateManagedItems(editorUi, graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue)

{




    var systemEntityTexts = systemEntityText.split("__");
    var systemEntityValues = systemEntityValue.split("__");






    var systemEntityTexts_data = ""
    for (var r = 0; r < systemEntityTexts.length; r++) {
        systemEntityTexts_data += systemEntityTexts[r] + "__";
    }
    var systemEntityTextsv = systemEntityTexts_data.split("__");



    var systemEntityValues_data = ""
    for (var e = 0; e < systemEntityValues.length; e++) {
        systemEntityValues_data += systemEntityValues[e] + "__";
    }


    var systemEntityValuesv = systemEntityValues_data.split("__");




    var div = document.createElement('div');
    div.appendChild(createItem("Managed Items", "", "thtitle"));

    var addmore = mxUtils.button("Add Item", function() {

        getSesstion("topolgy_systems", function(mvalue) {
            mvalue = mvalue.split("__");
            $(".morehere:last").after(dataForRow(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue, "xx", mvalue));
            updateEntity(systemEntityTextsv, systemEntityValuesv);
        });



    });


    addmore.setAttribute('class', 'addmore');
    addmore.setAttribute('style', 'float:right;clear:both;with:100%');

    div.appendChild(addmore);


    var divd1 = document.createElement('div');
    divd1.setAttribute('style', 'clear:both;with:100%');

    var table = document.createElement('table');
    table.style.width = '100%';
    table.style.height = '100%';
    var tbody = document.createElement('tbody');


    row = document.createElement('tr');
    var tds = createLabel('Managed Item Name:');
    row.appendChild(tds);
    tds = createLabel('Sql Name:');
    row.appendChild(tds);
    tds = createLabel('Collsion:');
    row.appendChild(tds);
    tds = createLabel('System:');
    row.appendChild(tds);
    tds = createLabel('Entity:');
    row.appendChild(tds);
    tbody.appendChild(row);

    row = document.createElement('tr');
    row.setAttribute('class', 'morehere');

    tbody.appendChild(row);

    table.appendChild(tbody);

    divd1.appendChild(table);
    div.appendChild(divd1);



    var myUp = mxUtils.button("Update", function() {

        $(".top_Managed_Item_more").remove();

        $(".morehere").each(function() {

            var Managed_Item_Name = $(this).children("td").children(".Managed_Item_Name").val();
            var Name_Sql_Name = $(this).children("td").children(".Name_Sql_Name").val();
            var Collsion = $(this).children("td").children(".Collsion").val();
            var System = $(this).children("td").children(".System").val();
            var Entity = $(this).children("td").children(".Entity").val();
            var value = "Managed_Item_Name__Name_Sql_Name__Collsion__System__Entity;" + Managed_Item_Name + "__" + Name_Sql_Name + "__" + Collsion + "__" + System + "__" + Entity


            var myadddata = returnInputField("top_Managed_Item_more", value);

            $(".itmedata").append(myadddata);

            getmDirectionXml();


            alert("Managed Items has been updated.");

        });

        // editorUi.hideDialog();


    });
    myUp.setAttribute('class', 'myupdate');


    div.appendChild(myUp);


    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "file.js";
    script.onload = function() {
        me_doOnLoad(graph, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue);
    };
    div.appendChild(script);

    this.container = div;

}
